from django import forms
from web.views.account import BootstragForm
from scripts.init_price_police import S28
from django.core.exceptions import ValidationError
from web.forms.my_color_widget import ClolrRadioSelect


class ProjectModelForm(forms.Form, BootstragForm):
    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request
    project_name = forms.CharField(label='项目名称', max_length=20, error_messages=[('required', '不能为空')])
    color = forms.ChoiceField(label='颜色', choices=(('1', '#FF4500'), ('2', '#FFFF00'), ('3', '#00FF00')), widget=ClolrRadioSelect(attrs={'class': 'color-radio'}))
    desc = forms.CharField(label='项目描述', max_length=300, widget=forms.Textarea(attrs={'style': 'resize:none;', 'xx': 123}))
    #print('错误信息')
    #print(color.choices)
    def clean_project_name(self):
        """项目校验"""
        print(self)
        print(self.request)
        project_name = self.cleaned_data.get('project_name')
        user_id = self.request.user_id
        ret = S28().py_searche('select {0} from {1} where project_name="{2}" and creator_id={3}'.format('*', 'project', str(project_name), str(user_id)))
        if ret:
            raise ValidationError('该项目已经存在了')
        use_num = S28().py_searche('select count(*) from project where creator_id="{0}"'.format(user_id))
        if use_num[0][0] >= self.request.project_num:
            raise ValidationError('您已经创建了5个项目了 可以办取会员继续创建')

        return project_name